﻿<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html>
<!-- saved from url=(0033)http://research.microsoft.com/pex -->
<head><link rel="StyleSheet" href="er.common.css" type="text/css" /><script src="er.common.js" type="text/javascript"> </script><link rel="SHORTCUT ICON" href="favicon.ico" /><link rel="icon" href="favicon.ico" type="image/x-icon" /><title>C:\Users\AO\Documents\Visual Studio 2010\AutomaTones3\automatones\AutomaTones\AutomaTones\View\Midi Out View\MidiOut.cs</title></head><body><div class="banner"></div><h2 id="top">C:\Users\AO\Documents\Visual Studio 2010\AutomaTones3\automatones\AutomaTones\AutomaTones\View\Midi Out View\MidiOut.cs</h2><div class="toggle"><p class="copyright"><a class="usercodeundertestcovered">user code under test</a>, 
  <span class="usercodeundertestnotcovered">user code under test (not covered)</span>, 
  <a class="userortestcodecovered">user code or test</a>, 
  <span class="userortestcodenotcovered">user code or test (not covered)</span>,
  <span class="tagged">tagged</span></p><table><tr><th>Methods</th></tr><tr><th /></tr></table><div class="toggle"><a class="button" href="#ID0EL">first</a><pre class="coverage">using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using AutomaTones.HW_Sense_Compute;
using AutomaTones.Model.Automaton_Model;
using AutomaTones.Model.MIDI;
using Midi;
using Microsoft.Pex.Framework;

namespace AutomaTones.View.Midi_Out_View {
    /// &lt;summary&gt;
    /// MidiOut is holding a reference to the virtual midi driver, LoopBe.
    /// Which lets the program communicate with other programs through standard MIDI.
    /// &lt;/summary&gt;
    /// &lt;author&gt;Anders Bech Mellson, anbh@itu.dk&lt;/author&gt;
    [PexClass]
    class MidiOut : IHardwareView {
        private Channel _channel;
        private IMapper _mapper;
        private int _volume;

        /// &lt;summary&gt;
        /// MidiOut constructor, that initialises key components for use.
        /// Also the constructor opens the Midi output, if it is not already open.
        /// &lt;/summary&gt;
        /// &lt;param name="channel"&gt;The Midi channel the MidiOut will send messages on.&lt;/param&gt;
        /// &lt;param name="mapper"&gt;The mapper that MidiOut will use to translate State[,] grid's into Midi notes.&lt;/param&gt;
        <span id="ID0EL"><a class="button" href="#top">top</a></span><a class="userortestcodecovered">public MidiOut(Channel channel, IMapper mapper)</a> <a class="userortestcodecovered">{</a>
            Contract.Requires(mapper != null);
            <a class="userortestcodecovered">_channel = channel;</a>
            <a class="userortestcodecovered">_mapper = mapper;</a>
            <a class="tagged" title="call to uninstrumented method OutputDevice.get_IsOpen()">if (!ConnectVirtualMidi.Out.IsOpen)</a>
                <a class="userortestcodecovered">ConnectVirtualMidi.Out.Open();</a>
            <a class="userortestcodecovered">_volume = 100;</a>
        <a class="userortestcodecovered">}</a>

        /// &lt;summary&gt;
        /// Transforms a State[,] grid into Midi notes that is sent to the connected output.
        /// The transformation is being calculated by the mapper.
        /// &lt;/summary&gt;
        /// &lt;param name="stateGrid"&gt;Grid to be played by MidiOut.&lt;/param&gt;
        [PexMethod]
        public void SetGrid(State[,] stateGrid) {
            //Contract.Requires(stateGrid!=null);
            List&lt;NoteOut&gt; notesToPlay = _mapper.Translate(stateGrid);
            foreach (NoteOut note in notesToPlay) {
                ConnectVirtualMidi.Out.SendNoteOn(_channel,note.Pitch,_volume*note.Velocity/100);
                if (note.Controller != 0)
                    ConnectVirtualMidi.Out.SendControlChange(_channel, Control.Volume + note.Controller,
                                                             note.ControllerValue);
            }
        }

        /// &lt;summary&gt;
        /// Silences all the notes being played by this MidiOut.
        /// &lt;/summary&gt;
        [PexMethod]
        public void ClearGrid() {
            ConnectVirtualMidi.Out.SilenceAllNotes();
        }

        /// &lt;summary&gt;
        /// Change the MidiOutput channel.
        /// &lt;/summary&gt;
        /// &lt;param name="channel"&gt;The Channel that the MidiOut should send on&lt;/param&gt;
        [PexMethod]
        public void SetChannel(Channel channel) {
            _channel = channel;
        }

        /// &lt;summary&gt;
        /// Change the Midi out volume relatively compared to the velocity of each note.
        /// If you send 100% then the notes will play at their normal velocity,
        /// sending lower volumes will turn each notes velocity down.
        /// &lt;/summary&gt;
        /// &lt;param name="volume"&gt;Volume in %.&lt;/param&gt;
        [PexMethod]
        public void SetVolume(int volume) {
            Contract.Requires(volume&lt;=100 &amp;&amp; volume &gt;=0);
            _volume = volume;
        }

        /// &lt;summary&gt;
        /// Change the mapper on the MidiOut. The mapper is in charge of the way that
        /// Cellular Automatones gets translated into Midi notes.
        /// Have fun with trying different mappers.
        /// &lt;/summary&gt;
        /// &lt;param name="mapper"&gt;The new mapper you wish to assign to this MidiOut.&lt;/param&gt;
        [PexMethod]
        public void SetMapper(IMapper mapper) {
            Contract.Requires(mapper != null);
            _mapper = mapper;
        }


        /// &lt;summary&gt;
        /// Inherited method from IHardwareView.
        /// Could convert one state to a MidiNote,
        /// but it wasn't needed in the end as our design changed.
        /// &lt;/summary&gt;
        [PexMethod]
        public void SetCell(int x, int y, State state)
        {
            throw new NotImplementedException();
        }

        /// &lt;summary&gt;
        /// Inherited method from IHardwareView.
        /// Not implemented here, because I couldn't figure out a use for it.
        /// &lt;/summary&gt;
        [PexMethod]
        public void SetArbitraryGrid(State[,] stateGrid)
        {
            throw new NotImplementedException();
        }

        [ContractInvariantMethod]
        private void Invariant() {
            Contract.Invariant(_mapper!=null);
        }
    }
}
</pre></div></div><hr /><table width="100%"><tr><td valign="top"><span class="copyright">Copyright (c) Microsoft Corporation. All rights reserved.</span><br /><span class="button" onclick="copySourceToClipboard()">Copy full source to clipboard</span></td></tr></table><div id="debugdiv"> </div></body></html>